package com.itheima.reggie.conrtroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.OrderDetailSerivce;
import com.itheima.reggie.service.OrderSerivce;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrderSerivce orderSerivce;
    @Autowired
    private OrderDetailSerivce orderDetailSerivce;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderSerivce.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 手机端查看订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
        ordersWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersWrapper.orderByDesc(Orders::getOrderTime);
        orderSerivce.page(ordersPage,ordersWrapper);
        //获取所有订单明细
        BeanUtils.copyProperties(ordersPage, dtoPage, "records");
        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> list = records.stream().map(record-> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            Long id = record.getId();
            LambdaQueryWrapper<OrderDetail> ordersDetailWrapper = new LambdaQueryWrapper<>();
            ordersDetailWrapper.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> orderDetails = orderDetailSerivce.list(ordersDetailWrapper);
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);

    }

    /**
     * 后端分页展示订单
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page>page(int page, int pageSize,String number,String beginTime,String endTime){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderTime);
        wrapper.like(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        if (StringUtils.isNotEmpty(beginTime)&&StringUtils.isNotEmpty(endTime)){
            wrapper.between(Orders::getOrderTime,beginTime,endTime);
        }

        orderSerivce.page(ordersPage,wrapper);
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> collect = records.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();

            BeanUtils.copyProperties(item, ordersDto);
            User byId = userService.getById(item.getUserId());
            ordersDto.setUserName(byId.getPhone());
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(collect);

        return R.success(ordersDtoPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<Orders>update(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId,orders.getId());
        Orders one = orderSerivce.getOne(wrapper);
        one.setStatus(orders.getStatus());
        orderSerivce.updateById(one);
        return R.success(one);
    }

    @PostMapping("/again")
    public R<String>again(@RequestBody Orders orders){
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailSerivce.list(wrapper);
        List<ShoppingCart> shoppingCarts=list.stream().map(item->{
//创建购物车对象
            ShoppingCart shoppingCart = new ShoppingCart();
            //设置菜品名
            shoppingCart.setName(item.getName());
            //设置图片
            shoppingCart.setImage(item.getImage());
//        设置用户
            shoppingCart.setUserId(BaseContext.getCurrentId());
            //设置菜品
            shoppingCart.setDishId(item.getDishId());
            //设置套餐id
            shoppingCart.setSetmealId(item.getSetmealId());
//        设置口味
            shoppingCart.setDishFlavor(item.getDishFlavor());
            //设置数量
            shoppingCart.setNumber(item.getNumber());
//        设置金额
            shoppingCart.setAmount(item.getAmount());
            return shoppingCart;

        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("添加成功");
    }
}
